<template>
  <div class="autumn-container">
    <!-- 面包 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/dashboard' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/system/department/departList' }">部门列表</el-breadcrumb-item>
      <el-breadcrumb-item>部门信息</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <div class="autumn-center">
      <div class="edit-form">
        <el-form ref="editFormRef" size="large" label-width="90px" :model="editForm" :rules="editFormRules">
          <el-row>
            <el-col :span="12">
              <el-form-item label="上级" prop="parentId">
                <autumn-tree-select :data="treeDeparts" v-model="editForm.parentId" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="编码" prop="code">
                <el-input v-model="editForm.code" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="名称" prop="name">
                <el-input v-model="editForm.name" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="类型" prop="type">
                <el-select v-model="editForm.type">
                  <el-option label="部门" value="1" />
                  <el-option label="岗位" value="2" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="排序" prop="sort">
                <el-input-number v-model="editForm.sort" :min="1" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="负责人" prop="manager">
                <el-input v-model="editForm.manager" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="副负责人" prop="deputy">
                <el-input v-model="editForm.deputy" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系电话" prop="phone">
                <el-input v-model="editForm.phone" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="传真" prop="fax">
                <el-input v-model="editForm.fax" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="邮箱" prop="email">
                <el-input v-model="editForm.email" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input type="textarea" resize="none" v-model="editForm.remark" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

        <div class="form-foot-button">
          <el-button type="primary" @click="submit(editFormRef)">保存</el-button>
          <el-button @click="cancel">取消</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { FormInstance } from 'element-plus'
import { $convertTree } from 'src/util/tools'
import { listDepart, viewDepart, saveDepart, updateDepart } from 'src/api/system/department'

const editFormRef = ref<FormInstance>()

const treeDeparts = ref<Array<any>>([])
listDepart().then(({ data }) => {
  treeDeparts.value = $convertTree(data)
})

// 定义表单数据
const editForm = reactive({
  id: '',
  parentId: '',
  code: '',
  name: '',
  type: '1',
  sort: '10',
  manager: '',
  deputy: '',
  phone: '',
  fax: '',
  email: '',
  remark: ''
})
const editFormRules = {
  code: [
    { required: true, message: '请输入部门编码', trigger: 'blur' }
  ],
  name: [
    { required: true, message: '请输入部门名称', trigger: 'blur' }
  ],
  sort: [
    { required: true, message: '请输入排序', trigger: 'blur' }
  ]
}

// 修改时填充表单数据
const route = useRoute()
if (route.query.id) {
  const id = route.query.id as string
  viewDepart(id).then(({ data }) => {
    Object.assign(editForm, data)
  })
}

// 提交
const submit = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(valid => {
    if (!valid) return
    if (!editForm.id) {
      saveDepart(editForm).then(() => {
        cancel()
      })
    } else {
      updateDepart(editForm).then(() => {
        cancel()
      })
    }
  })
}

// 取消
const router = useRouter()
const cancel = () => {
  router.push('/system/department/departList')
}

</script>

<style lang="scss" scoped></style>
